<?php
if (!defined('ABSPATH')) { exit; }
// 简易选项卡容器：记录管理 / 链接规则 / PRO版本
$tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : 'rules';
?>
<div class="wrap">
    <h1>插件设置</h1>
    <h2 class="nav-tab-wrapper">
        <a href="#settings-rules" class="nav-tab spa-tab-link <?php echo $tab==='rules'?'nav-tab-active':''; ?>" data-tab="rules">链接规则</a>
        <a href="#settings-pro" class="nav-tab spa-tab-link <?php echo $tab==='pro'?'nav-tab-active':''; ?>" data-tab="pro">PRO版本</a>
    </h2>

    <div class="tab-panel">
        <?php
        $map = array(
            'rules'   => WP_SPIDER_MONITOR_PLUGIN_PATH . 'templates/rules-page.php',
            'pro'     => WP_SPIDER_MONITOR_PLUGIN_PATH . 'templates/pro-page.php'
        );
        $template = isset($map[$tab]) ? $map[$tab] : $map['rules'];
        if (file_exists($template)) {
            // 使用输出缓冲防止外部错误导致白屏
            ob_start();
            include $template;
            $content = ob_get_clean();
            echo $content;
        } else {
            echo '<div class="notice notice-error"><p>模板文件缺失：' . esc_html($template) . '</p></div>';
        }
        ?>
    </div>
</div>

<style>
.tab-panel .wrap { padding: 0; }

.spa-tab-link {
    cursor: pointer;
    transition: all 0.3s ease;
}

.spa-tab-link:hover {
    background-color: #f1f1f1;
}

.nav-tab-active {
    background-color: #fff;
    border-bottom-color: #fff;
}
</style>

<script>
jQuery(document).ready(function($) {
    // 处理SPA标签页切换
    $('.spa-tab-link').on('click', function(e) {
        e.preventDefault();
        
        const tab = $(this).data('tab');
        const $this = $(this);
        
        // 更新活动状态
        $('.spa-tab-link').removeClass('nav-tab-active');
        $this.addClass('nav-tab-active');
        
        // 加载对应标签页内容
        loadTabContent(tab);
    });
    
    // 简易本地缓存：避免重复请求
    const tabCache = {};
    function loadTabContent(tab) {
        const $tabPanel = $('.tab-panel');
        if (tabCache[tab]) { $tabPanel.html(tabCache[tab]); return; }
        $tabPanel.html('<div style="text-align: center; padding: 40px;"><div class="spinner is-active"></div><p>加载中...</p></div>');
        $.post(ajaxurl || window.ajaxurl, {
            action: 'spider_get_settings_tab',
            tab: tab,
            nonce: wpSpiderMonitor?.nonce || ''
        })
        .done(function(response) {
            if (response && response.success) {
                tabCache[tab] = response.data.content || '';
                $tabPanel.html(tabCache[tab]);
            } else {
                $tabPanel.html('<div class="notice notice-error"><p>加载失败: ' + (response?.data?.message || '未知错误') + '</p></div>');
            }
        })
        .fail(function(xhr, status, error) {
            $tabPanel.html('<div class="notice notice-error"><p>请求失败: ' + error + '</p></div>');
        });
    }
    
    // 初始化时加载当前标签页
    const currentTab = $('.nav-tab-active').data('tab') || 'records';
    if (currentTab) {
        loadTabContent(currentTab);
    }
});
</script>

